package com.smart.utils;


import cn.hutool.json.JSONUtil;
import com.smart.common.entities.ajax.AjaxResult;
import lombok.extern.log4j.Log4j2;

import javax.servlet.ServletResponse;
import java.io.PrintWriter;

/**
 * 响应工具累
 *
 * @author Administrator
 * @date 2022/10/23
 */
@Log4j2
public class ResponseUtil {


    /**
     * 使用response输出JSON
     */
    public static void out(ServletResponse response, AjaxResult<?> result) {
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            out.println(JSONUtil.toJsonStr(result));
        } catch (Exception e) {
            log.error(e.getMessage());

        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
        }
    }


}